package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.admin.vo.CommonVo;
import com.cskaoyan.service.MarketIssueService;
import com.cskaoyan.utils.CommonUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (MarketIssue)表控制层
 *
 * @author makejava
 * @since 2021-10-18 23:41:30
 */
@RestController
@RequestMapping("admin/issue")
public class MarketIssueController {
    /**
     * 服务对象
     */
    @Resource
    private MarketIssueService marketIssueService;

    /**
     * 分页查询
     *
     * @param
     * @param
     * @return 查询结果
     */
    @RequiresPermissions("admin:issue:list")
    @GetMapping("list")
    public BaseRespVo list(BaseParam param, String question) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        String q = null;
        if (question != null)q = "%" + question + "%";
        List<MarketIssue> marketIssues =marketIssueService.queryByPage(q,param);
        int size = marketIssues.size();
        CommonVo commonVo = new CommonVo();
        commonVo.setList(marketIssues);
        commonVo.setTotal(size);
        commonVo.setPage(param.getPage());
        commonVo.setLimit(param.getLimit());
        commonVo.setPages(size / param.getLimit() + 1);
        return BaseRespVo.ok(commonVo, "成功");
    }


    /**
     * 新增数据
     *
     * @param marketIssue 实体
     * @return 新增结果
     */
    @RequiresPermissions("admin:issue:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketIssue marketIssue) {
        if (CommonUtil.isNull(marketIssue.getAnswer())||CommonUtil.isNull(marketIssue.getQuestion())){
            return BaseRespVo.fail("不可以为空");
        }
        //设置时间在sql语句中,用自增主键注解
        MarketIssue insert = (MarketIssue) marketIssueService.insert(marketIssue);
        insert.setId(insert.getId());
        insert.setAddTime(CommonUtil.nowTime());
        insert.setUpdateTime(CommonUtil.nowTime());
        return BaseRespVo.ok(insert);
    }

    /**
     * 编辑数据
     *
     * @param marketIssue 实体
     * @return 编辑结果
     */
    @RequiresPermissions("admin:issue:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketIssue marketIssue) {
        if (CommonUtil.isNull(marketIssue.getAnswer())||CommonUtil.isNull(marketIssue.getQuestion())){
            return BaseRespVo.fail("不可以为空");
        }
        //设置时间在sql语句中
        MarketIssue update = (MarketIssue) marketIssueService.update(marketIssue);
        update.setUpdateTime(CommonUtil.nowTime());
        return BaseRespVo.ok(update);
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @RequiresPermissions("admin:issue:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketIssue marketIssue) {
        marketIssueService.deleteById(marketIssue.getId());
        return BaseRespVo.ok();
    }

}

